package com.example.cslp.service.Impl;

import com.example.cslp.exception.handler.ServiceException;
import com.example.cslp.mapper.CourseMapper;
import com.example.cslp.pojo.DTO.CourseDTO;
import com.example.cslp.pojo.VO.CourseListItemVO;
import com.example.cslp.pojo.entity.Course;
import com.example.cslp.service.ICourseService;
import com.example.cslp.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class CourseServiceImpl implements ICourseService {

    @Autowired
    private CourseMapper courseMapper;

    /**
     * 通过课程id修改课程审核状态
     * @param courseDTO 课程实体
     */
    @Override
    public List<CourseListItemVO> list() {
        List<CourseListItemVO> list = courseMapper.list();
        return list;
    }

    @Override
    public void audit(CourseDTO courseDTO)  {
        Course course = new Course();
        BeanUtils.copyProperties(courseDTO,course);
        //设置最后审核时间
        course.setReviewTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        int rows = courseMapper.audit(course);
        if(rows!=1){
            String message="更新课程信息失败";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    /**
     * 返回审核通过的课程列表
     * @return
     */
    @Override
    public List<CourseListItemVO> audited() {
        List<CourseListItemVO> list = courseMapper.auditedList();
        return list;
    }

    /**
     * 返回审核不通过的课程列表
     * @return
     */
    @Override
    public List<CourseListItemVO> auditFailed() {
        List<CourseListItemVO> list = courseMapper.auditFailedList();
        return list;
    }

    /**
     * 返回待审核的课程列表
     * @return
     */
    @Override
    public List<CourseListItemVO> notAudited() {
        List<CourseListItemVO> list = courseMapper.notAuditedList();
        return list;
    }

    @Override
    public List<CourseListItemVO> query(String courseName) {
        List<CourseListItemVO> courseListItemVOS = courseMapper.pageList(courseName);
        return courseListItemVOS;
    }

    @Override
    public void addNew(Course course) {
        courseMapper.addNew(course);
        System.out.println("新增课程成功！");
    }

}
